package com.sskj.xyft.transaction;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.xyft.MVPFragment;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.xyft.R;
import com.sskj.xyft.transaction.data.OrderBean;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/03/20
 */
public class GuaDanFragment extends MVPFragment<GuaDanPresenter> {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private BaseAdapter<OrderBean.Order> baseAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_gua_dan;
    }

    @Override
    public GuaDanPresenter getPresenter() {
        return new GuaDanPresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initView() {
        initRefreshLayout(smartRefreshLayout);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setPaintWidth(20)
                .setDividerColor(Color.parseColor("#00ffffff"))
        );
        baseAdapter = new BaseAdapter<OrderBean.Order>(R.layout.item_guadan, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, OrderBean.Order item) {
                holder.setText(R.id.tv_type, item.getBuyBillType() == 1 ? "做多" : "做空")
                        .setText(R.id.tv_name, item.getStockName())
                        .setText(R.id.tv_buy_price, item.getPrice())
                        .setText(R.id.tv_hand_times, item.getEntrustCount() + "")
                        .setText(R.id.tv_promise_money, item.getBuyPrincipal())
                        .setText(R.id.tv_service_charge, item.getFee())
                        .setText(R.id.tv_stop_profit, item.getStopProfitPrice())
                        .setText(R.id.tv_create_time, item.getCreateTime())
                        .setText(R.id.tv_stop_loss, item.getStopLossPrice());
                if (item.getBuyBillType() == 1) {
                    holder.setTextColor(R.id.tv_type, color(R.color.red));
                } else {
                    holder.setTextColor(R.id.tv_type, color(R.color.green));
                }
                //撤单
                ClickUtil.click(holder.getView(R.id.btn_close_order), view -> {
                    new AlertDialog.Builder(getContext())
                            .setTitle("提示")
                            .setMessage("确定撤销此订单吗？")
                            .setPositiveButton("确定", (dialog, which) -> {
                                dialog.dismiss();
                                mPresenter.cancelOrder(getAccount(), item.getId() + "");
                            }).setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
                            .show();
                });

            }
        };

    }


    @Override
    public void initData() {

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (isLogin()) {
                mPresenter.getGuaDanList(getAccount());
            }
        }
    }


    public static GuaDanFragment newInstance() {
        GuaDanFragment fragment = new GuaDanFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    public void setData(OrderBean data) {
        if (data != null) {
            baseAdapter.setNewData(data.getList());
        } else {
            baseAdapter.setNewData(null);
        }
    }

    public void cancelOrderSuccess() {
        ToastUtils.show("撤销订单成功");
        mPresenter.getGuaDanList(getAccount());
        if (getParentFragment() != null) {
            TransactionHomeFragment fragment = (TransactionHomeFragment) getParentFragment();
            fragment.getPresenter().getAccountDetail(getAccount(), true);
        }
    }
}
